﻿OB.Emes_ActionButton = OB.Emes_ActionButton || {};

//质量判定
OB.Emes_ActionButton.Quality_OrderTable_Judgment = function(params, win) {
	var grid = win.view.viewGrid;
	var record = grid.getSelectedRecord();
	
	OB.Emes_ActionButton.orderOrderTableOpenWindow = win;
	OB.Emes_ActionButton.orderOrderTableWindow = isc.Emesq_Quality_OrderTable_JudgmentWindow.create(record);
	OB.Emes_ActionButton.orderOrderTableWindow.show();
};

var Quality_OrderTableView_Init_Data = {
	title: "质量判定",
	width: 700,
	height: 330,
	align: 'center',
	defaultLayoutAlign: 'center',
	resultMap: {},
	reasonMap: {},
	allItem: [],
	orderId: null,
	submit: function(isForce){
		var _self = this;
		var code = this.jobOrderCodeQtyForm.getValue("jobOrderCode");
		if(code == null || code == ""){
			isc.showMessage("作业订单号不能为空", isc.OBMessageBar.TYPE_ERROR);
			return;
		}
		var result = this.jobQualityInfo.getValue("result");
		if (!result) {
			isc.showMessage("请选择质检结果！", isc.OBMessageBar.TYPE_ERROR);
			return;
		}
		var reason = this.jobQualityInfo.getValue("reason");
		if (result != "QUALIFIED" && !reason) {
			isc.showMessage("请选不良原因！", isc.OBMessageBar.TYPE_ERROR);
			return;
		}
		var description = this.jobQualityInfo.getValue("description");
		
		if (this.submitLoading) {
			return;
		}
		isc.confirm("确定判定为" + this.resultMap[result] + "？", function(bool) {
			if (bool) {
				var requestData = {
					method: "submit",
					code: code,
					result: result,
					reason: reason,
					entityName: _self.entityName,
					description: description
				};
				_self.submitLoading = true;
				OB.RemoteCallManager.call("com.mes.handler.job.EmesqQualityOrderTableHander",requestData,{},
					function(response, data, request){
						_self.submitLoading = false;
						if(data.result){
							if (OB.Emes_ActionButton.orderOrderTableOpenWindow) {
								OB.Emes_ActionButton.orderOrderTableOpenWindow.refresh();
							}
							if (OB.Emes_ActionButton.orderOrderTableWindow) {
								OB.Emes_ActionButton.orderOrderTableWindow.closeClick();
							} else {
								_self.clearForm();
							}
							isc.showMessage("提交成功！", isc.OBMessageBar.TYPE_INFO);
						}else{
							isc.showMessage(data.text, isc.OBMessageBar.TYPE_ERROR);
						}
					}
				);
			}
		});
	},
	initWidget: function (data) {
		this.entityName = data._entityName;
		
		//OB控件 追加	addField or addItem	会有样式和事件找不到的问题
		this.createJobOrderForm(data);
		this.createForm();
		
		var pageLayout = isc.VLayout.create({
			width: '100%',
			height: 330,
			layoutMargin : 5,
			membersMargin : 10,
			members : [
				this.jobOrderInfoLayout,
				this.jobQualityInfo,
				this.buttonLayout
			]
		});
		
		//却别弹框与直接菜单打开方式
		if (data && data.code) {
			this.items = [pageLayout];
		} else {
			this.addMembers(pageLayout);
		}
		this.Super("initWidget", arguments);
		
		this.initDataSelect();
		
		if (data && data.code) {
			this.orderData = data;
			this.jobOrderForm.search();
		} else {
			this.clearForm();
		}
	},
	createJobOrderForm: function(data) {
		var _self = this;
		this.jobOrderCodeQtyForm = isc.DynamicForm.create({
			ID: "jobOrderCodeQtyForm",
			width: 300,
			numCols : 2,
			fields: [
				{width: '*',type: 'text',title: '报工单号',name: 'jobOrderCode' ,value: (data && data.code ? data.code : ""),startRow: true,
				keyDown: function(field, form, keyName){
					if(keyName == "Enter"){
						jobOrderForm.search();
						return false;
					}
					return true;
				}}
			]
		});
		this.jobOrderForm = isc.DynamicForm.create({
			ID: "jobOrderForm",
			width: '100%',
			numCols: 4,
			search: function(){
				var code = jobOrderCodeQtyForm.getValue("jobOrderCode");
				if(code == '') {
					isc.showMessage("请输入订单编码",isc.OBMessageBar.TYPE_INFO);
					return;
				}
				var requestData = {
					method: "getData",
					entityName: _self.entityName,
					code: code
				};
				
				OB.RemoteCallManager.call("com.mes.handler.job.EmesqQualityOrderTableHander",requestData,{},function(response, data, request){
					_self.clearForm();
					if(data && data.result){
						//只读默认值
						
						if (data.orderInfo) {
							_self.setValue(data.orderInfo, jobOrderForm);
							_self.setJobOrderStatus(data.orderInfo.orderStatus);
							_self.routeNo = data.orderInfo.routeNo;
							
							if (data.orderInfo.orderStatus == "OK") {
								_self.jobQualityInfo.getField("description").setValue(data.orderInfo.description);
								_self.jobQualityInfo.getField("result").setValue(data.orderInfo.qualityResult);
								_self.qualityResult = data.orderInfo.qualityResult;
								if (data.orderInfo.qualityResult != "QUALIFIED" && data.orderInfo.reason) {
									_self.defaultReason = data.orderInfo.reason;
									_self.jobQualityInfo.getField("reason").setValue(data.orderInfo.reason);
									_self.jobQualityInfo.getField("reason").show();
								}
							}
						}
						_self.jobOrderCodeQtyForm.getField("jobOrderCode").setCanEdit(false);
						//_self.orderId = code;
						_self.orderId = data.code;
						_self.jobQualityInfo.show();
						_self.buttonLayout.show();
						
					} else {
						isc.showMessage(data.text, isc.OBMessageBar.TYPE_INFO);
					}
				});
			},
			fields : [
				{width: '*',name:'group',type: 'OBSectionItem',defaultValue:'作业订单信息',sectionExpanded: true,colSpan: 6,itemIds: 
					['manuOrderCode', 'productCode', 'productName', 'routeName', 'qty', 'orderStatus', 'description']},
				{width: '*',type: 'text',title:'生产订单',	name: 'manuOrderCode',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'工序名称',	name: 'routeName',		canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'物料编号',	name: 'productCode',	canEdit:false, startRow: true, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'物料名称',	name: 'productName',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'图号',		name: 'name2',			canEdit:false, startRow: true, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'状态',		name: 'orderStatus',	canEdit:false, startRow: false, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'质检数量',	name: 'qty',			canEdit:false, startRow: true, readOnlyDisplay: "static"},
				{width: '*',type: 'text',title:'备注',		name: 'description',	canEdit:false, startRow: true, readOnlyDisplay: "static", colSpan: 4, width: '100%'}
			]
		});
		
		var processStandardBtn = isc.OBFormButton.create({
			ID:'processStandardBtn',
			title : '工艺标准',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewDrawingsOrderHandler",
						{ routeNo : _self.routeNo , id :_self.orderId },
						{},
						function(response, data, request) {
							if(data.flag){
								isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
							}else{
								top.isc.showMessage("该工序未上传工序标准。",top.isc.OBMessageBar.TYPE_SUCCESS);
							}
							
							
						});
			}
		});
		var productPictureBtn = isc.OBFormButton.create({
			ID:'productPictureBtn',
			title : '产品图纸',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewmaterialOrderHandler",
						{id :_self.orderId },
						{},
						function(response, data, request) {
							if(data.flag){
								isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
							}else{
								isc.showMessage(data.message);
							}
							
							
						});
			}
		});
		var jobInstructionsBtn = isc.OBFormButton.create({
			ID:'jobInstructionsBtn',
			title : '作业指导书',
			action : function() {
				OB.RemoteCallManager.call("com.mes.handler.ViewprocessOrderHandler",
						{ routeNo : _self.routeNo , id :_self.orderId },
						{},
						function(response, data, request) {
							if(data.flag){
								isc.EMES_Window_View.create({url: "DownFileServlet.html?path=" + encodeURIComponent(data.meg) + "&online=true&fileName=" + _self.routeNo + ".pdf"}).show();
							}else{
								isc.showMessage(data.message);
							}
							
							
						});
			}
		});
		this.jobOrderInfoLayout = isc.HLayout.create({
			width: '100%',
			height: 80,
			layoutMargin: 0,
			memberMargin: 5,
			members:[
			 	isc.VLayout.create({
					width: '75%',
					layoutMargin: 0,
					memberMargin: 0,
					align: 'left',
					defaultLayoutAlign: 'left',
					members:[
						jobOrderCodeQtyForm,
					 	this.jobOrderForm
					]
				}),
			 	isc.VLayout.create({
					width: '25%',
					layoutMargin: 0,
					memberMargin: 0,
					align: 'left',
					defaultLayoutAlign: 'left',
					members:[
						processStandardBtn,
						productPictureBtn,
						jobInstructionsBtn
					]
				})
			]
		});
	},
	createForm: function() {
		var _self = this;
		this.jobQualityInfo = isc.DynamicForm.create({
			ID: "jobQualityInfo",
			numCols : 8,
			fields : [
				{width: '*', type: "OBSectionItem", name: 'group', defaultValue:'质量判定', sectionExpanded: true, colSpan: 8, itemIds:["result", "reason", "description"]},
				{width: '*', type: 'select', title: '质检结果<span style="color: #ff0000">*</span>', name: 'result',	 
				 canEdit: true, startRow: false, valueMap: {},
					change: function(form, field, value) {
						var field = form.getField("reason");
						try{
							if (value == "QUALIFIED" || value == "") {
								field.hide();
								field.setValue("");
								field.setCanEdit(false);
							} else {
								field.show();
								field.setCanEdit(true);
							}
						} catch (e) {
						}
					}},
				{width: '*', type: 'select', title: '不良原因<span style="color: #ff0000">*</span>', name: 'reason',
				 canEdit: true, startRow: false, valueMap: {}, showIf: false},
				{width: '*', type: 'textarea', title: '备注', name: 'description', canEdit: true, startRow: true, colSpan: 3, height: 40}
			]
		});
		this.submitOkBtn = isc.OBFormButton.create({
			ID:'submitOkBtn',
			title : '提交',
			action : function() {
				_self.submit(false);
			}
		});
		
		this.cancelBtn = isc.OBFormButton.create({
			ID:'cancelBtn',
			title : '取消',
			action : function() {
				OB.Emes_ActionButton.orderOrderTableWindow.closeClick();
			}
		});
		this.buttonLayout = isc.HLayout.create({
			width: '100%',
			height: 40,
			layoutMargin: 15,
			memberMargin: 30,
			align: 'center',
			defaultLayoutAlign: 'center',
			members:[
		 		this.submitOkBtn,
				this.cancelBtn
			]
		});
	},
	clearForm: function(){
		this.orderId = null;
		
		jobQualityInfo.hide();
		this.buttonLayout.hide();
		jobOrderCodeQtyForm.getField("jobOrderCode").setCanEdit(true);
		
	},
	setValue: function(data, form){
		var itemIds = form.getFields();
		for(var i = 0 ; i < itemIds.length; i ++){
			var column = itemIds[i];
			this.setFieldValue(form.getField(column.name), data[column.name]);
		}
	},
	setFieldValue: function(field, value) {
		if (field.readOnlyDisplay = "static") {
			field.setReadOnlyDisplay("");
			field.setValue(value);
			field.setReadOnlyDisplay("static");
		} else {
			field.setValue(value);
		}
	},
	setJobOrderStatus: function(value) {
		var status = "";
		if(value == 'CREATE'){
			status = "创建";
		} else if(value == 'START'){
			status = "已开工";
		} else if(value == 'FINISHED'){
			status = "已完工";
		} else if(value == 'OK'){
			status = "已质检";
		} else if(value == 'STOP'){
			status = "已暂停";
		}
		this.setFieldValue(jobOrderForm.getField("orderStatus"), status);
	},
	initDataSelect: function() {
		var _self = this;
		OB.RemoteCallManager.call("com.basis.handler.GetReferenceListHandler",
			{id: "0B80A589DFD64147AD0126954CE05B8A"},
			{},
			function(response, data, request) {
				if(data.result){
					_self.resultMap = {};
					for (var key in data.list) {
						_self.resultMap[key] = data.list[key];
					}
					_self.jobQualityInfo.getField("result").setValueMap(_self.resultMap);
					
					if (_self.qualityResult) {
						_self.jobQualityInfo.getField("result").setValue(_self.qualityResult);
					}
				}else{
					isc.showMessage(data.text, isc.OBMessageBar.TYPE_ERROR);
				}
			}
		);
		OB.RemoteCallManager.call("com.mes.handler.job.EmesqQualityOrderTableHander",
			{method: "getReason"},
			{},
			function(response, data, request) {
				if(data.result){
					_self.reasonMap = {};
					for (var key in data.reasonList) {
						_self.reasonMap[key] = data.reasonList[key];
					}
					_self.jobQualityInfo.getField("reason").setValueMap(_self.reasonMap);
					if (_self.defaultReason) {
						_self.jobQualityInfo.getField("reason").setValue(_self.defaultReason);
						_self.jobQualityInfo.getField("reason").show();
					} else {
						_self.jobQualityInfo.getField("reason").hide();
					}
				}else{
					isc.showMessage(data.text, isc.OBMessageBar.TYPE_ERROR);
				}
			}
		);
	}
};

isc.defineClass("Emesq_Quality_OrderTable_JudgmentView", isc.OBBaseView).addProperties(Quality_OrderTableView_Init_Data);
isc.defineClass("Emesq_Quality_OrderTable_JudgmentWindow", isc.OBPopup).addProperties(Quality_OrderTableView_Init_Data);
